<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Auctions\Auction as Auctions;
use App\Models\Auctions\Zhuanchang;
use App\Models\Auctions\Art;
use Carbon\Carbon;
class Auction extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'auction:status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
		// return $this->cz();
        $this->zhuanchang();
        $this->auction();
    }
    //更新拍卖状态
    public function auction()
    {
        $list = Auctions::where('status', '<>', 1)->get();
        $carbon = new Carbon();
        $today = $carbon->toDateString();
        
        foreach ($list as $key => $value) {
            # code...
            $preview_start_time = Carbon::parse($value->preview_start_time);
            $preview_end_time = Carbon::parse($value->preview_end_time);
            $start_time = Carbon::parse($value->start_time);
            $end_time = Carbon::parse($value->end_time);
            if($carbon->between($preview_start_time, $preview_end_time) && $carbon->between($start_time, $end_time))
            {
                $value->status = 4;
                
            }elseif($carbon->between($preview_start_time, $preview_end_time))
            {
                $value->status = 2;
            }elseif($carbon->between($start_time, $end_time))
            {
                $value->status = 3;
            }elseif($carbon->gt($end_time))
            {
                //更新成交额
                // $this->arts($value);
                $value->sum_all = Zhuanchang::where('auctions_id', $value->id)->where('is_show', 1)->sum('sum_all');
                $value->status = 1;
            }else
            {
                $value->status = 0;
            }
            //计算专场数量
            $value->count_zhuanchangs = Zhuanchang::where('is_show', 1)->where('is_show', 1)->count();
            $value->count_yu_zhuanchangs = Zhuanchang::where('is_show', 1)->where('status', '<>', 1)->where('is_show', 1)->count();
            $value->count_jie_zhuanchangs = $value->count_zhuanchangs - $value->count_yu_zhuanchangs;
            $value->save();
            // if($value->preview_start_time)
        }
    }
	public function cz()
	{
		$list = Zhuanchang::whereIn('auctions_id', [6306,6249])->get();
		foreach ($list as $key => $value) {
			$this->arts($value);
			// $value->count_arts = Art::where('zc_id', $value->id)->where('status', 1)->count();
            // $value->save();
			echo $value->id.chr(10);
		}
	}
    // 更新专场状态
    public function zhuanchang()
    {
        $list = Zhuanchang::where('status', '<>', 1)->where('is_show', 1)->get();
        // dd($list->toArray());
        $carbon = new Carbon();
        foreach ($list as $key => $value) {
            # code...
            $start_time = Carbon::parse($value->start_time);
            $end_time = $start_time->addHours(10);
            if($carbon->between($start_time, $end_time))
            {
                $value->status = 3;
            }elseif($carbon->gt($end_time))
            {
                //更新成交额
                $this->arts($value);
                $value->status = 1;
            }else
            {
                $value->status = 0;
            }
            //计算专场有多少作品
            $value->count_arts = Art::where('zc_id', $value->id)->where('status', 1)->count();
            $value->save();
            echo $value->id.chr(10);
            // if($value->preview_start_time)
        }
    }
    //更新成交额
    public function arts($row)
    {
        $total = 0;
        $rate = 0;
        $count = $row->arts_all->count();
        if($count > 0)
        {
            foreach ($row->arts_all as $key => $value) {
                # code...
                if($value->price_rmb > 0)
                {
                    $rate += 1;
                }
                $total += $value->price_rmb;
            }
        }
        $row->sum_all = $total;
        if($count > 0)
        {
            $row->rate = number_format($rate/$count, 2);
        }else{
            $row->rate = 0;
        }
        $row->save();

        
    }
}
